﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

using AjaxControlFramework.Compilation;

namespace AjaxControlFramework
{
    public class CompiledScriptReference : ScriptReference
    {
        //------// Properties \\--------------------------------------------\\
        private string _sourcePath = String.Empty;
        public virtual string SourcePath
        {
            get { return _sourcePath; }
            set
            {
                if (value == null)
                {
                    _sourcePath = String.Empty;
                }
                else
                {
                    _sourcePath = value;
                }
            }
        }


        private ScriptType _scriptType = ScriptType.File;
        public virtual ScriptType ScriptType
        {
            get { return _scriptType; }
            set { _scriptType = value; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public CompiledScriptReference(string name, string sourcePath, ScriptType scriptType)
            : base(name)
        {
            SourcePath = sourcePath;
            ScriptType = scriptType;
        }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public override string GenerateScriptBlock()
        {
            StringBuilder scriptBlock = null;

            switch (ScriptType)
            {
                case ScriptType.File:
                case ScriptType.Embedded:

                    string sourceUrl = String.Empty;

                    Page currentPage = HttpContext.Current.CurrentHandler as Page;
                    if (currentPage != null)
                    {
                        sourceUrl = currentPage.ResolveClientUrl(SourcePath);
                    }

                    scriptBlock = new StringBuilder(50 + sourceUrl.Length);
                    scriptBlock.Append("<script type=\"text/javascript\" src=\"").Append(base.Name).Append(".js\"></script>");


                    // Register an HttpHandler in the in-memory web.config cache for this file.
                    JsxScript jsxScript = JsxScriptRegistry.GetScript(SourcePath);

                    if (jsxScript != null)
                    {
                        RuntimeConfigUtils.AddHttpHandler(base.Name + ".js", "GET", jsxScript.HandlerType.FullName);
                    }

                    break;

                case ScriptType.Inline:



                    break;
            }


            if (scriptBlock == null) 
            { 
                return String.Empty;
            }
            else
            {
                return scriptBlock.ToString();
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}